function ajax(url,method,data){
    method = method || 'GET';
    data = data||{};
    return new Promise((r,j)=>{
        var aj = new XMLHttpRequest;
        aj.onreadystatechange = function(){
            if (aj.readyState === 4) {
                if(aj.status === 200){
                    try{
                        var data = JSON.parse(aj.responseText);
                        if(data.error) throw data.msg;
                        r(data)
                    }catch(e){
                        j(e);
                    }
                }else{
                    j('网络错误')
                }
            }
        }
        aj.open(method,url,true);
        aj.setRequestHeader('Content-Type','application/x-www-form-urlencoded')
        aj.setRequestHeader('AJAX',1);
        var postData = [];
        for (const key in data) {
            const val = data[key];
            postData.push(key+'='+encodeURIComponent(val));
        }
        aj.send(postData.join('&'));
    })
}


HTMLDivElement.prototype.showModal = function(){
    this.style.display = 'block';
    var height = this.offsetHeight;
    this.style.top = `calc(40vh - ${height/2}px)`
}

HTMLDivElement.prototype.show = function(){
    this.style.display = 'block';
}

HTMLDivElement.prototype.hide = function(){
    this.style.display = 'none';
}

HTMLInputElement.prototype.warning  = function(){
    this.style.borderColor='red';
}
HTMLInputElement.prototype.normal  = function(){
    this.style.borderColor='#ccc';
}

document.addEventListener('DOMContentLoaded',()=>{
    document.addEventListener('click',ev=>{
        switch(true){
            case ev.target.matches('.modal'):
            case ev.target.matches('.action-sheel'):
                // ev.target.parentNode.removeChild(ev.target)
                ev.target.hide();
                break;
        }
    })
})


// 根据微信提供的特殊属性判断是否在微信内置浏览器中
function isWeChat() {
    return window.hasOwnProperty('WeixinJSBridge') || window.hasOwnProperty('WeChatJSBridge');
}